package com.yungu.swift.order.helper;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.assets.passenger.model.dto.PassengerDto;
import com.yungu.swift.assets.passenger.service.PassengerLogService;
import com.yungu.swift.assets.passenger.service.PassengerService;
import com.yungu.swift.constants.PassengerConstant;
import com.yungu.swift.system.sys.model.dto.SysAccountPassengerDto;
import com.yungu.swift.system.sys.service.SysAccountPassengerService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * @author : cuixiuyin
 * @date : 2019/11/1
 */
@Component
public class PassengerHelper {

    private static PassengerService passengerService;

    private static PassengerLogService passengerLogService;

    private static SysAccountPassengerService sysAccountPassengerService;

    public static PassengerDto newPassenger(String mobile, String name, String appid) {
        PassengerDto passengerDto = new PassengerDto();
        passengerDto.setUuid(StringUtils.buildUUID());
        passengerDto.setStatus(PassengerConstant.PASSENGER_STATUS_NORMAL);
        passengerDto.setOrderCount(0);
        passengerDto.setMobile(mobile);
        passengerDto.setSex(PassengerConstant.SEX_MAN);
        passengerDto.setToken(StringUtils.buildUUID());
        passengerDto.setUuid(StringUtils.buildUUID());
        passengerDto.setCreateOn(new Date());
        passengerDto.setLastLogin(new Date());
        passengerDto.setAppid(appid);
        passengerDto.setActive(1);
        passengerDto.setNickname(name);
        passengerService.add(passengerDto);

        // 记录乘客注册日志，默认为 APP 注册
        Map<String, Object> regParams = MapUtils.build(8);
        regParams.put("uuid", StringUtils.buildUUID());
        regParams.put("appid", appid);
        regParams.put("passengerUuid", passengerDto.getUuid());
        regParams.put("action", 1);
        passengerLogService.saveRegisterLog(regParams);

        //同步新增乘客账户
        SysAccountPassengerDto accountPassengerDto = new SysAccountPassengerDto();
        accountPassengerDto.setUuid(passengerDto.getUuid());
        accountPassengerDto.setAppid(appid);
        accountPassengerDto.setAccountName(name);
        accountPassengerDto.setPresentBalance(BigDecimal.ZERO);
        accountPassengerDto.setRechargeBalance(BigDecimal.ZERO);
        accountPassengerDto.setAlipayAccumulate(BigDecimal.ZERO);
        accountPassengerDto.setTenpayAccumulate(BigDecimal.ZERO);
        accountPassengerDto.setCreateOn(new Date());
        sysAccountPassengerService.add(accountPassengerDto);
        return passengerDto;
    }

    @Reference
    public void setPassengerService(PassengerService passengerService) {
        PassengerHelper.passengerService = passengerService;
    }

    @Reference
    public void setPassengerLogService(PassengerLogService passengerLogService) {
        PassengerHelper.passengerLogService = passengerLogService;
    }

    @Reference
    public void setSysAccountPassengerService(SysAccountPassengerService sysAccountPassengerService) {
        PassengerHelper.sysAccountPassengerService = sysAccountPassengerService;
    }
}
